<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" href="/nfwt.ico" />
    <link href="https://unpkg.com/element-plus@1.2.0-beta.3/dist/index.css" rel="stylesheet"/>
    <script src="https://unpkg.com/vue@3.2.31/dist/vue.global.js"></script>
    <script src="https://unpkg.com/element-plus@2.1.4/dist/index.full.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>运行代码</title>
  </head>
  <body>
    <div id="app"></div>
    <script >
      // 使用 eval编译js代码
      const mysetup = `
        (function setup (props, ctx) {
          {{code}}
        })
      `
      // 接收URL的参数
      const search = decodeURI(window.location.search)
      const id = search.split('&')[0].replace('?id=','')
      // 接收代码
      const code = top.window.__code[id]
      // 设置模板
      const temp = code.template

      const {
        defineComponent,
        defineAsyncComponent,
        ref,
        reactive,
        watch,
        watchEffect,
        computed,
        shallowReactive,
        shallowReadonly,
        shallowRef,
        readonly,
        toRaw,
        isRef,
        isProxy,
        isReactive,
        isReadonly,
        toRef,
        toRefs,
        nextTick
      } = Vue
      
      const App = {
        template: temp, // 设置模板
        setup (_props, _ctx) {
          const tmpJs = code.js // 获取js代码
          let fun = null // 转换后的函数
          try {
            if (tmpJs)
              fun = eval(mysetup.replace('{{code}}', tmpJs)) // 用 eval 把 字符串 变成 函数
          } catch (error) {
            console.error('转换出现异常：', error)
          }
          const re = typeof fun === 'function' ? fun : () => {}

          return {
            ...re(_props, _ctx) // 运行函数，解构返回对象
          }
        }
      }
      const app = Vue.createApp(App)
      app.use(ElementPlus).mount("#app")
    </script>
  </body>
</html>
